from apps.common.base.views import *
from apps.education.course.serializers import *
from apps.education.course.dao import CourseDao
from apps.education.course.filters import CourseFilter
from apps.education.courseStudent.dao import CourseStudentDao

class BaseCourseViewSet(BaseViewSet):
    serializer_class = CourseModelSerializer
    filter_class = CourseFilter
    DAO_CLASS = CourseDao

    @action(methods=['post'], detail=False)
    def getItemById(self, request):
        params = getRequestParams(self.request)
        try:
            obj_id = params.get('obj_id')
            if not obj_id:
                return Response(response_message('obj_id参数必填'))
            obj = self.DAO_CLASS().findOneByObjId(obj_id)
            model = self.serializer_class(obj).data
            # 用户报名状态
            courseStudents = CourseStudentDao().findAll().filter(course=obj, student__user=request.user)
            if courseStudents.exists():
                model['memberCheckStatus'] = courseStudents.first().checkStatus
            
        except VerifyException as e:
            return Response(response_message(e.message))
        return Response(response_success(model))